package com.zhang.file.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.common.oss.minio.FileInfoVo;
import com.zhang.common.oss.minio.MinioUtil;
import com.zhang.file.dto.FileSearchDTO;
import com.zhang.file.entity.Files;
import com.zhang.file.mapper.FileMapper;
import com.zhang.file.service.IFileAction;
import com.zhang.file.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文件信息表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-07-28
 */
@Service
@Slf4j
public class FileServiceImpl extends ServiceImpl<FileMapper, Files> implements IFileService {
    @Autowired
    private IFileAction fileAction;
    @Value("${minio.endpoint}")
    private String endpoint;

    @Override
    public InputStream getFile(String bucket, String filePath) {
//获取文件后缀名
//        String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
//        String name = fileName.substring(filePath.lastIndexOf(".") + 1);
        try {
            return fileAction.readFile(bucket, filePath);
        } catch (Exception e) {
            log.error("文件读取失败----->", e);
            return null;
        }
    }

    @Override
    public List<String> getAllFileBucket() throws Exception {
        return fileAction.listBuckets();
    }

    @Override
    public Page getFileFromBucket(FileSearchDTO searchDTO) throws Exception {
        QueryWrapper<Files> wrapper = new QueryWrapper<>();
        wrapper.eq("file_bucket", searchDTO.getBucketName());
        return page(new Page<Files>(searchDTO.getPageNum(), searchDTO.getPageSize()), wrapper);
    }

    @Override
    public Files uploadFile(MultipartFile file, FileInfoVo fileInfo) {
        Files files = new Files();
        try {
            log.info("输出上传文件名：{}，类型:{}", file.getOriginalFilename(), file.getContentType());
            String fileName = RandomUtil.randomString(6) + file.getOriginalFilename();
//            String imageName = file.getOriginalFilename();
            String fullName = fileInfo.getFolderName() + "/" + fileName;
            /*先上传，然后文件信息入库*/
            fileAction.uploadFile(file.getInputStream(), fileInfo.getBucketName(), fullName);
            files.setFileBucket(fileInfo.getBucketName());
            files.setFileName(fileName);
            files.setFileUrl(fullName);
            files.setFilePath(fileInfo.getBucketName() + "/" + fullName);
            files.setFileSize(file.getSize());
            files.setFileType(file.getContentType());
            save(files);//存入数据库
        } catch (Exception e) {
            /*注意这里有两个异常，你需要查案是哪一个哦*/
            log.error("文件上传出现错误，注意这里有两三个个异常，你需要查案是哪一个哦，因为我都放到一个异常捕获了->", e);
        }
        return files;
    }

    @Override
    public void delFileByFileId(List<Long> fileIds) {
        for (Long fileId : fileIds) {
            Files byId = getById(fileId);
            removeById(fileId);
            try {
                fileAction.deleteFile(byId.getFileBucket(), byId.getFileUrl());
            } catch (Exception e) {
                log.error("删除文件失败", e);
            }
        }
    }
}
